{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "0dc1f3a7",
      "metadata": {},
      "outputs": [],
      "source": [
        "# 通义千问（langchain-community 集成版）\n",
        "from langchain_community.llms import Tongyi\n",
        "from langchain_core.messages import HumanMessage, SystemMessage\n",
        "\n",
        "QWEN_API_KEY= 'sk-db3036a8653249a98e764882a7c649fb'\n",
        "\n",
        "llm = Tongyi(api_key=QWEN_API_KEY)\n",
        "\n",
        "messages = [\n",
        "    SystemMessage(content=\"Translate the following from English into Italian\"),\n",
        "    HumanMessage(content=\"hi!\"),\n",
        "]\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "print(llm(\"用一句话介绍通义千问\"))\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "7c5eebea",
      "metadata": {},
      "outputs": [],
      "source": [
        "llm.invoke(messages)\n",
        "# 流式输出\n",
        "async for chunk in llm.astream(\"用一句话介绍通义千问,不少于100个字\"):\n",
        "    print(chunk, end=\"|\", flush=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "54457017",
      "metadata": {},
      "outputs": [],
      "source": [
        "from langchain_core.output_parsers import JsonOutputParser\n",
        "\n",
        "# \n",
        "chain = (\n",
        "    llm | JsonOutputParser()\n",
        ")  # Due to a bug in older versions of Langchain, JsonOutputParser did not stream results from some models\n",
        "async for text in chain.astream(\n",
        "    \"output a list of the countries france, spain and japan and their populations in JSON format. \"\n",
        "    'Use a dict with an outer key of \"countries\" which contains a list of countries. '\n",
        "    \"Each country should have the key `name` and `population`\"\n",
        "):\n",
        "    print(text, flush=True)\n",
        "    "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d6f7cc8b",
      "metadata": {},
      "outputs": [],
      "source": [
        "from langchain_core.prompts import ChatPromptTemplate\n",
        "\n",
        "system_template = \"Translate the following into {language}:\"\n",
        "\n",
        "prompt_template = ChatPromptTemplate.from_messages(\n",
        "    [(\"system\", system_template), (\"user\", \"{text}\")]\n",
        ")\n",
        "\n",
        "# result = prompt_template.invoke({\"language\": \"italian\", \"text\": \"hi\"})\n",
        "\n",
        "# result\n",
        "# result.to_messages()\n",
        "\n",
        "# llm.invoke({\"language\": \"italian\", \"text\": \"hi\"})\n",
        "chain = prompt_template | llm \n",
        "chain.invoke({\"language\": \"italian\", \"text\": \"hi\"})\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ee9296d9",
      "metadata": {},
      "outputs": [],
      "source": [
        "import langchain_core\n",
        "\n",
        "langchain_core.__version__\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "02ccca0d",
      "metadata": {},
      "source": [
        "# 事件"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c7ce589c",
      "metadata": {},
      "outputs": [],
      "source": [
        "events = []\n",
        "async for event in llm.astream_events(\"hello\", version=\"v2\"):\n",
        "    events.append(event)\n",
        "events[-2:]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "66b108dd",
      "metadata": {},
      "outputs": [],
      "source": [
        "chain = llm.with_config({\"run_name\": \"model\"}) | JsonOutputParser().with_config(\n",
        "    {\"run_name\": \"my_parser\"}\n",
        ")\n",
        "\n",
        "max_events = 0\n",
        "async for event in chain.astream_events(\n",
        "    \"output a list of the countries france, spain and japan and their populations in JSON format. \"\n",
        "    'Use a dict with an outer key of \"countries\" which contains a list of countries. '\n",
        "    \"Each country should have the key `name` and `population`\",\n",
        "    version=\"v2\",\n",
        "    #按名称过滤\n",
        "    include_names=[\"my_parser\"],\n",
        "):\n",
        "    print(event)\n",
        "    max_events += 1\n",
        "    if max_events > 10:\n",
        "        # Truncate output\n",
        "        print(\"...\")\n",
        "        break"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "28d86a57",
      "metadata": {},
      "outputs": [],
      "source": [
        "chain = llm.with_config({\"run_name\": \"model\"}) | JsonOutputParser().with_config(\n",
        "    {\"run_name\": \"my_parser\"}\n",
        ")\n",
        "\n",
        "\n",
        "max_events = 0\n",
        "async for event in chain.astream_events(\n",
        "    'output a list of the countries france, spain and japan and their populations in JSON format. Use a dict with an outer key of \"countries\" which contains a list of countries. Each country should have the key `name` and `population`',\n",
        "    version=\"v2\",\n",
        "    # 按类型过滤\n",
        "    # include_types=[\"chat_model\"],\n",
        "    include_types=[\"parser\"],\n",
        "):\n",
        "    print(event)\n",
        "    max_events += 1\n",
        "    if max_events > 10:\n",
        "        # Truncate output\n",
        "        print(\"...\")\n",
        "        break"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ee995b9a",
      "metadata": {},
      "outputs": [],
      "source": [
        "chain = (llm | JsonOutputParser()).with_config({\"tags\": [\"my_chain\"]})\n",
        "\n",
        "max_events = 0\n",
        "async for event in chain.astream_events(\n",
        "    'output a list of the countries france, spain and japan and their populations in JSON format. Use a dict with an outer key of \"countries\" which contains a list of countries. Each country should have the key `name` and `population`',\n",
        "    version=\"v2\",\n",
        "    # 按标签过滤\n",
        "    include_tags=[\"my_chain\"],\n",
        "):\n",
        "    print(event)\n",
        "    max_events += 1\n",
        "    if max_events > 10:\n",
        "        # Truncate output\n",
        "        print(\"...\")\n",
        "        break"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "62c127eb",
      "metadata": {},
      "source": [
        "# 传播回调"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "56906c3f",
      "metadata": {},
      "outputs": [],
      "source": [
        "from langchain_core.runnables import RunnableLambda\n",
        "from langchain_core.tools import tool\n",
        "\n",
        "\n",
        "def reverse_word(word: str):\n",
        "    return word[::-1]\n",
        "\n",
        "\n",
        "reverse_word = RunnableLambda(reverse_word)\n",
        "\n",
        "\n",
        "@tool\n",
        "def bad_tool(word: str):\n",
        "    \"\"\"Custom tool that doesn't propagate callbacks.\"\"\"\n",
        "    return reverse_word.invoke(word)\n",
        "\n",
        "\n",
        "async for event in bad_tool.astream_events(\"hello\", version=\"v2\"):\n",
        "    print(event)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "511695f3",
      "metadata": {},
      "source": [
        "# 客户端访问"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "1fb99c91",
      "metadata": {},
      "outputs": [],
      "source": [
        "# 客户端访问LangServer\n",
        "\n",
        "from langserve import RemoteRunnable\n",
        "\n",
        "remote_chain = RemoteRunnable(\"http://localhost:8000/chain/\")\n",
        "remote_chain.invoke({\"language\": \"chinese\", \"text\": \"long\"})"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "myenv",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.18"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}